home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10330 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: ix.netcom.com!netnews
  2. From: Mike Girou <girou@parashift.com>
  3. Newsgroups: comp.lang.c++,rb.technical
  4. Subject: Re: What is "placement new" - was Re: Can copy constructor and operator= share code?
  5. Date: Thu, 07 Mar 1996 11:26:16 -0600
  6. Organization: Paradigm Shift, Inc.
  7. Message-ID: <313F1C38.462A@parashift.com>
  8. References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <VA.00000053.00cdab05@fred> <NICKC.96Mar7142042@uxe.liv.ac.uk>
  9. NNTP-Posting-Host: ix-dfw9-03.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-NETCOM-Date: Thu Mar 07  9:26:35 AM PST 1996
  14. X-Mailer: Mozilla 2.0 (Win95; I)
  15.  
  16. Spider plant breeding program wrote:
  17. > In article <VA.00000053.00cdab05@fred> Frederic LACHASSE <lachass@worldnet.fr> writes:
  18. > > From: Frederic LACHASSE <lachass@worldnet.fr>
  19. > > Generally, the operator=() must release old resource and create a copy
  20. > > of the other object. So a generic operator=() can be:
  21. > >
  22. > > T &T::operator =(const T &t)
  23. > > {
  24. > >   if (this != &t) // if objects are same, nothing to do
  25. > >   {
  26. > >     ~T(); // explicit call to destructor to release resources.
  27. > Shouldn't this be       this->~T();
  28. > >     new(this) T(t); // use of the placement operator to call the
  29. > >                     // copy constructor.
  30. > >   }
  31. >                         return *this;
  32. > > }
  33. > >
  34. > > Some times though, the assignement operator can be optimize to reuse
  35. > > resources of the old object.
  36. > What is ths placement new operator?
  37. > It's mentioned in passing in the FAQ, but never defined.
  38. > No compiler I can run recognises this syntax
  39. > (I've tried gcc 2.6.3, Solaris C++ (V4 I think), Irix C++)
  40.  
  41. Placement new is a way to do a "new" off of previously allocated
  42. memory, and is important for efficiency reasons, such as buffer
  43. management.  See Stroustrup's fundamental book for a good description.
  44.  
  45. Mike
  46.  
  47. > The problem I'm trying to solve is:
  48. > class a
  49. > {
  50. >   int *i;
  51. >   const double &d;      // Actually something more complex
  52. >   public:
  53. >   a( const double &d_ ) : i( new int[3] ), d( d_ ) {};
  54. >   ~a() { delete i; };
  55. > };
  56. > How do I write operator = ( const class &a )    ?
  57. > Thanks in advance,
  58. > Nicholas Clark
  59. > --
  60. > C:\> ECHO f 0000:0000 ffff 66 | DEBUG
  61.  
  62. -- 
  63. Mike Girou              girou@parashift.com
  64.